function loadPage(DivId, action, serviceId){
	$('#' + DivId).hide();
	activeNav(action);
	$.ajax({
		type:	"GET",
		url:	action + "?set_ajax_view",
		success: function(data){
			$('#'+ DivId).html(data);
			$('#'+ DivId).show('slow');
			if(action != 'CreateCalendarAjax'){
				getUsersByEmployeeType();
			}
		}
	});
}

function activeNav(linkId) {
	$('#calNav a').removeClass('active');
	$('#' + linkId).addClass('active');
}

function toggleCheck(me, prefix) {
	var inputArray = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
	if ($('#' + me).attr('checked') == true) {
		for (var i = 0; i < inputArray.length; i++) {
			$('#' + prefix + '_' + inputArray[i]).attr('checked', 'checked');
		}
	} else {
		for (var i = 0; i < inputArray.length; i++) {
			$('#' + prefix + '_' + inputArray[i]).removeAttr('checked');
		}
	}	
}

function getUsersByEmployeeType(){
	$('#dayWeek_form').hide();
	$('#user_tbl tbody').html('');
	$('#search_div').hide();
	var employee_type_id = $('#select_employee_type').val();
	$.ajax({
		type:	"GET",
		url:	"GetUserByEmployeeTypeAjax?set_ajax_view",
		data:	"employee_type_id=" + employee_type_id ,
		success: function(data){
			$('#select_employee').html('');
			$('#select_employee').append("<option id='' value=''>-- " + _("Select employee") + " --</option>");
			$('#select_employee').append(data);
		}
	});
}

function renderWeek(action, year, mon, day){
	if($('#select_employee').val() == ''){
		$('#dayWeek_form').hide();
		$('#save_btn').hide();
		return false;
	}
	$.ajax({
		type: 	"GET",
		data: 	"personId=" + $('#select_employee').val() + "&year=" + year
				+ "&mon=" + mon + "&day=" + day + "&actionCalendar=" + action,
		url:	"GetCalendarTblAjax?set_ajax_view",
		success: function(data){
			$('#dayWeek_form').hide();
			$('#dayWeek_form').html(data);
			$('#dayWeek_form').show();
			$('#save_btn').show();
		}
	});
}

function saveCalendar(){
	var submitData = $('#dayWeek_form').serialize();
	$('#dayWeek_form').hide('slow');
	$('#save_btn').hide('slow');
	$.ajax({
		type:	"GET",
		data:	submitData + "&personId=" + $('#select_employee').val(), 
		url:	"SaveCalendarAjax?set_ajax_view",
		success: function(data){
			$('#calendar_id').val(data);
			$.ajax({
				type: 	"GET",
				data: 	"personId=" + $('#select_employee').val() + "&year=&mon=&day=&actionCalendar=edit",
				url:	"GetCalendarTblAjax?set_ajax_view",
				success: function(data){		
					$('#dayWeek_form').html(data);
					$('#dayWeek_form').show('slow');
					$('#save_btn').show('slow');
				}
			});
		}
	});
}

function getRelationalUsers(){
	var employeeId = $('#select_employee').val();
	if(employeeId == ''){
		$('#search_div').hide();
		$('#user_tbl tbody').html('');
		return false;
	}
	
	$.ajax({
		type:	"GET",
		url:	"GetRelationalUserAjax?set_ajax_view",
		data:	"employeeId=" + employeeId ,
		success: function(data){
			$('#user_tbl tbody').html('');
			$('#user_tbl tbody').append(data);
		}
	});
	$('#search_div').show();
	getEmployeeToAdd();
}

function getEmployeeToAdd(){	
	$('#search_Result').hide();
	$.ajax({
			type:	"GET",
			url:	"GetEmployeeToAddAjax?set_ajax_view",
			data: 	"personId=" + $('#select_employee').val() + "&employeeTypeId=" + $('#employee_type_search').val() ,
			success: function(data){
				if(data == ''){
					$('#searchResult').hide('');
					$('#search_message').text(_("Result not found."));
				}else{
					$('#search_message').text("");
					$("#searchResultTbl tbody").html('');
					$("#searchResultTbl tbody").append(data);
					$('#searchResult').show('slow');
				}
			}
		});
		return;
}

function addUser(userId, rowId) {
	var name = $('#'+userId+'_name_span').text();
	var employee_type = $('#employee_type_search :selected').text();
	
	var data_row = "<tr id="+userId+"_row><td class='first'>"+name+"<input type='hidden' name='user[]' value='"+userId+"'/></td>"
	     + "<td>"+employee_type+"</td>"
	 	 + "<td center class='last'><a href='javascript:removeUser("+userId+");'>" + _("Unselect") + "</a></td></tr>";
	$.ajax({
		type:	"GET",
		url:	"addUserToRelationshipAjax?set_ajax_view",
		data:	"relational_personId=" + userId + "&personId=" + $('#select_employee').val(),
		success: function(data){
			if(data == '1'){
				$('#'+rowId+'_row').remove();
			return ;
			}else{
				$('#user_tbl tbody').append(data_row);
				$('#'+rowId+'_row').remove();
				$('#user_list_tbl').hide();
				$('#user_list_tbl').show();
			}
		}
	});
}

function removeUser(userId) {
	$.ajax({
		type:	"GET",
		url:	"RemoveUserFromRelationshipAjax?set_ajax_view",
		data:	"relational_personId=" + userId + "&personId=" + $('#select_employee').val(),
		success: function(data){
			getEmployeeToAdd();
		}
	});
	$('#'+userId+'_row').remove();
}

function editNote(personId, date) {
	$('#editNoteForm').hide();
	$('#editContentLoading').show();
	$('#editNoteDialog').dialog('open');
	$('#notePerson').val(personId);
	$('#noteDate').val(date);
	$.ajax({
		type:	"GET",
		url:	"getNoteAjax?set_ajax_view",
		data:	"personId=" + personId + "&date=" + date,
		dataType: "json",
		success: function(data){
			$('#noteContent').val(data.note);
			$('#noteId').val(data.id);
			$('#editContentLoading').hide();
			$('#editNoteForm').show();
		}
	});
}

function saveNote() {
	$('#editNoteForm').hide();
	$('#editContentLoading').show();
	var noteId = $('#noteId').val();
	var urlParam = "createNewNoteAjax?set_ajax_view";
	var noteContent = $.trim($('#noteContent').val());
	var dataParam = "personId=" + $('#notePerson').val() + "&date=" + $('#noteDate').val() + "&note=" + noteContent;
	if (noteId > 0) {
		urlParam = "updateNoteAjax?set_ajax_view";
		dataParam = "noteId=" + noteId + "&note=" + noteContent;
	} else {		
		if (noteContent == '') {			
			$('#editNoteDialog').dialog('close');
			return;			
		}
	}
	$.ajax({
		type:	"POST",
		url:	urlParam,
		data:	dataParam,
		success: function(data){
			$('#editNoteDialog').dialog('close');
		}
	});
}

function deleteNote() {
	$('#editNoteForm').hide();
	$('#editContentLoading').show();
	var noteId = $('#noteId').val();
	if (noteId > 0) {
		$.ajax({
			type:	"GET",
			url:	"deleteNoteAjax?set_ajax_view",
			data:	"noteId=" + noteId,
			success: function(data){
				$('#editNoteDialog').dialog('close');
			}
		});
	} else {
		$('#editNoteDialog').dialog('close');
	}
}

function viewNote(noteId) {
	$('#noteContentView').hide();
	$('#viewContentLoading').show();
	$('#viewNoteDialog').dialog('open');
	$.ajax({
		type:	"GET",
		url:	"viewNoteAjax?set_ajax_view",
		data:	"noteId=" + noteId,
		success: function(data){
			$('#noteContentView').html(data);
			$('#viewContentLoading').hide();
			$('#noteContentView').show();
		}
	});
}